﻿
//Author: 张亚涛
//createtime:2015-12-24 18:32:17
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Coupons" 表的方法;
    /// </summary>
    public class CouponsBase
    {
        const string SQL_Select = "select [ID],[OpenID],[Money],[Explain],[YouXQStart],[YouXQEnd],[CouType],[IsUse],[TradeID] from [Coupons] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回Coupons表的所有数据  
        /// </summary>
        /// <param name="OpenID">微信ID</param>
        /// <param name="IsUsable">可用标识</param>
        /// <param name="couType">类型</param>
        /// <param name="order">排序</param>
        /// <returns></returns>        
        public static List<Coupons> GetAllCoupons(string OpenID, int IsUsable, int couType, string order)
        {
            List<Coupons> couponsList = new List<Coupons>();
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [OpenID] = @OpenID ";
            if (IsUsable == 1)
            {
                sql += " and IsUse=0 and CONVERT(varchar(100), YouXQEnd, 23) >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "'";
            }
            if (couType != 9)
            {
                sql += " and CouType= '" + couType + "' ";
            }
            sql += " order by id " + order;
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@OpenID",OpenID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            while (reader.Read())
            {
                Coupons coupons = new Coupons(reader);
                couponsList.Add(coupons);
            }
            reader.Close();
            return couponsList;
        }
        /// <summary>
        /// 根据Coupons表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static Coupons GetCouponsById(long id)
        {
            Coupons coupons = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                coupons = new Coupons(reader);
            }
            reader.Close();
            return coupons;
        }
        /// <summary>
        /// 是否已使用
        /// </summary>
        /// <param name="ID">ID</param>
        /// <returns></returns>        
        public static bool IsUse(int ID)
        {
            string sql = "select IsUse from [Coupons] where ID='" + ID + "'";
            return SQLHelper.ExecuteScalar(sql, null) == "1";
        }
        /// <summary>
        /// 是否首次优惠
        /// </summary>
        /// <param name="OpenID">微信ID</param>
        /// <returns></returns>        
        public static bool IsFirst(string OpenID)
        {
            string sql = "select count(OpenID) from [Coupons] where OpenID='" + OpenID + "'";
            return int.Parse(SQLHelper.ExecuteScalar(sql, null)) == 0;
        }
        /// <summary>
        /// 返回优惠券可用数量
        /// </summary>
        /// <param name="openID">微信ID</param>
        /// <param name="couType">类型(0：优惠券1：洗车券)</param>
        /// <returns></returns>        
        public static int GetCount(string openID, int couType)
        {
            string sql = "select count(OpenID) from [Coupons] where IsUse=0 and CONVERT(varchar(100), YouXQEnd, 23) >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' and OpenID='" + openID + "' and CouType='" + couType + "'";
            return int.Parse(SQLHelper.ExecuteScalar(sql, null));
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据Coupons表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteCouponsById(long id)
        {
            string sql = "delete from [Coupons] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Coupons coupons)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",coupons.Id),
            new SqlParameter("@OpenID",coupons.OpenID),
            new SqlParameter("@Money",coupons.Money),
            new SqlParameter("@Explain",coupons.Explain),
            new SqlParameter("@YouXQStart",coupons.YouXQStart),
            new SqlParameter("@YouXQEnd",coupons.YouXQEnd),
            new SqlParameter("@CouType",coupons.CouType),
            new SqlParameter("@IsUse",coupons.IsUse),
            new SqlParameter("@TradeID",coupons.TradeID)
            };
            return paras;
        }
        /// <summary>
        /// 根据Coupons表的ID字段更新数据 
        /// </summary> 
        /// <param name="Coupons">coupons</param>
        /// <returns></returns>       
        public static int UpdateCouponsById(Coupons coupons)
        {
            string sql = "update [Coupons] set [OpenID] = @OpenID,[Money] = @Money,[Explain] = @Explain,[YouXQStart] = @YouXQStart,[YouXQEnd] = @YouXQEnd,[CouType] = @CouType,[IsUse] = @IsUse,[TradeID] = @TradeID where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(coupons);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Coupons表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="coupons">Coupons</param>       
        /// <returns></returns>        
        public static int AddCoupons(Coupons coupons)
        {
            string sql = "insert into [Coupons]([OpenID],[Money],[Explain],[YouXQStart],[YouXQEnd],[CouType],[IsUse],[TradeID])values(@OpenID,@Money,@Explain,@YouXQStart,@YouXQEnd,@CouType,@IsUse,@TradeID)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(coupons);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 向Coupons表插入一条数据（SQL语句）
        /// </summary>
        /// <param name="m">Coupons</param>       
        /// <returns></returns>        
        public static string AddCoupons_SQL(Coupons m)
        {
            string sql = "insert into [Coupons]([OpenID],[Money],[Explain],[YouXQStart],[YouXQEnd],[CouType],[IsUse],[TradeID])";
            sql += " values('" + m.OpenID + "','" + m.Money.ToString() + "','" + m.Explain + "','" + m.YouXQStart.ToString() + "','" + m.YouXQEnd.ToString() + "','" + m.CouType + "',0,'" + m.TradeID + "');";

            return sql;
        }
        /// <summary>
        /// 更新使用状态（SQL语句）
        /// </summary> 
        /// <param name="couponsID">CouponsID</param>
        /// <param name="isUse">是否使用</param>
        /// <returns></returns>       
        public static string UpdateUse_SQL(long couponsID, int isUse)
        {
            string sql = "update [Coupons] set [IsUse] = '" + isUse + "' where [ID] = '" + couponsID + "'";
            return sql;
        }
        /// <summary>
        /// 返回Coupons表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllCoupons_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}